Crate human_regex

source ·
Expand description

Github CI Crates.io docs.rs

Regex for Humans

The goal of this crate is simple: give everybody the power of regular expressions without having to learn the complicated syntax. It is inspired by ReadableRegex.jl. This crate is a wrapper around the core Rust regex library.

Example usage

If you want to match a date of the format 2021-10-30, you could use the following code to generate a regex:

use human_regex::{beginning, digit, exactly, text, end};
let regex_string = beginning()
    + exactly(4, digit())
    + text("-")
    + exactly(2, digit())
    + text("-")
    + exactly(2, digit())
    + end();
assert!(regex_string.to_regex().is_match("2014-01-01"));

The to_regex() method returns a standard Rust regex. We can do this another way with slightly less repetition though!

use human_regex::{beginning, digit, exactly, text, end};
let first_regex_string = text("-") + exactly(2, digit());
let second_regex_string = beginning()
    + exactly(4, digit())
    + exactly(2, first_regex_string)
    + end();
assert!(second_regex_string.to_regex().is_match("2014-01-01"));

For a more extensive set of examples, please see The Cookbook.

Features

This crate currently supports the vast majority of syntax available in the core Rust regex library through a human-readable API.

Single Character

Implemented?ExpressionDescription
any().any character except new line (includes new line with s flag)
digit()\ddigit (\p{Nd})
non_digit()\Dnot digit
unicode_category(UnicodeCategory)\p{L}Unicode non-script category
unicode_script(UnicodeScript)\p{Greek}Unicode script category
non_unicode_category(UnicodeCategory)\P{L}Negated one-letter name Unicode character class
non_unicode_script(UnicodeCategory)\P{Greek}negated Unicode character class (general category or script)

Character Classes

Implemented?ExpressionDescription
or(&['x', 'y', 'z']) [xyz]A character class matching either x, y or z (union).
nor(&['x', 'y', 'z'])[^xyz]A character class matching any character except x, y and z.
within('a'..='z')[a-z]A character class matching any character in range a-z.
without('a'..='z')[^a-z]A character class matching any character outside range a-z.
See below[[:alpha:]]ASCII character class ([A-Za-z])
non_alphanumeric()[[:^alpha:]]Negated ASCII character class ([^A-Za-z])
or()[x[^xyz]]Nested/grouping character class (matching any character except y and z)
and(&[])/&[a-y&&xyz]Intersection (a-y AND xyz = xy)
(or[1,2,3,4] & nor(3))[0-9&&[^4]]Subtraction using intersection and negation (matching 0-9 except 4)
subtract(&[],&[])[0-9--4]Direct subtraction (matching 0-9 except 4). Use .collect::<Vec> to use ranges.
xor(&[],&[])[a-g~~b-h]Symmetric difference (matching a and h only). Requires .collect() for ranges.
or(&escape_all(&['[',']']))[\[\]]Escaping in character classes (matching [ or ])

Perl Character Classes

Implemented?ExpressionDescription
digit()\ddigit (\p{Nd})
non_digit()\Dnot digit
whitespace()\swhitespace (\p{White_Space})
non_whitespace()\Snot whitespace
word()\wword character (\p{Alphabetic} + \p{M} + \d + \p{Pc} + \p{Join_Control})
non_word()\Wnot word character

ASCII Character Classes

Implemented?ExpressionDescription
alphanumeric()[[:alnum:]]alphanumeric ([0-9A-Za-z])
alphabetic()[[:alpha:]]alphabetic ([A-Za-z])
ascii()[[:ascii:]]ASCII ([\x00-\x7F])
blank()[[:blank:]]blank ([\t ])
control()[[:cntrl:]]control ([\x00-\x1F\x7F])
digit()[[:digit:]]digits ([0-9])
graphical()[[:graph:]]graphical ([!-~])
uppercase()[[:lower:]]lower case ([a-z])
printable()[[:print:]]printable ([ -~])
punctuation()[[:punct:]]punctuation ([!-/:-@\[-`{-~])
whitespace()[[:space:]]whitespace ([\t\n\v\f\r ])
lowercase()[[:upper:]]upper case ([A-Z])
word()[[:word:]]word characters ([0-9A-Za-z_])
hexdigit()[[:xdigit:]]hex digit ([0-9A-Fa-f])

Repetitions

Implemented?ExpressionDescription
zero_or_more(x)x*zero or more of x (greedy)
one_or_more(x)x+one or more of x (greedy)
zero_or_one(x)x?zero or one of x (greedy)
zero_or_more(x)x*?zero or more of x (ungreedy/lazy)
one_or_more(x).lazy()x+?one or more of x (ungreedy/lazy)
zero_or_more(x).lazy()x??zero or one of x (ungreedy/lazy)
between(n, m, x)x{n,m}at least n x and at most m x (greedy)
at_least(n, x)x{n,}at least n x (greedy)
exactly(n, x)x{n}exactly n x
between(n, m, x).lazy()x{n,m}?at least n x and at most m x (ungreedy/lazy)
at_least(n, x).lazy()x{n,}?at least n x (ungreedy/lazy)

Composites

Implemented?ExpressionDescription
+xyconcatenation (x followed by y)
or()x\|yalternation (x or y, prefer x)

Empty matches

Implemented?ExpressionDescription
beginning()^the beginning of text (or start-of-line with multi-line mode)
end()$the end of text (or end-of-line with multi-line mode)
beginning_of_text()\Aonly the beginning of text (even with multi-line mode enabled)
end_of_text()\zonly the end of text (even with multi-line mode enabled)
word_boundary()\ba Unicode word boundary (\w on one side and \W, \A, or \z on other)
non_word_boundary()\Bnot a Unicode word boundary

Groupings

Implemented?ExpressionDescription
capture(exp)(exp)numbered capture group (indexed by opening parenthesis)
named_capture(exp, name)(?P<name>exp)named (also numbered) capture group
Handled implicitly through functional composition(?:exp)non-capturing group
See below(?flags)set flags within current group
See below(?flags:exp)set flags for exp (non-capturing)

Flags

Implemented?ExpressionDescription
case_insensitive(exp)icase-insensitive: letters match both upper and lower case
multi_line_mode(exp)mmulti-line mode: ^ and $ match begin/end of line
dot_matches_newline_too(exp)sallow . to match \n
will not be implemented1Uswap the meaning of x* and x*?
disable_unicode(exp)uUnicode support (enabled by default)
will not be implemented2xignore whitespace and allow line comments (starting with #)
  1. With the declarative nature of this library, use of this flag would just obfuscate meaning.
  2. When using human_regex, comments should be added in source code rather than in the regex string.

Modules

Functions for ASCII character classes
Functions for capturing matches
A Cookbook of Common Tasks
Functions for directly matching text or adding known regex strings
Functions for the empty matches
Functions for adding flags
Functions for performing logical operations
Functions for matching repetitions
Functions for general purpose matches

Structs

The HumanRegex struct which maintains and updates the regex string. For most use cases it will never be necessary to instantiate this directly.

Enums

An enum covering all Unicode character categories
An enum for covering all Unicode script categories

Functions

A function to match any alphabetic character ([A-Za-z])
A function to match any alphanumeric character ([0-9A-Za-z])
A function for establishing an AND relationship between two or more possible matches
A function for matching any character (except for \n)
A function to match any ascii digit ([\x00-\x7F])
Match at least n of a certain target
A function to match the beginning of text (or start-of-line with multi-line mode)
A function to match the beginning of text (even with multi-line mode enabled)
Match at least n and at most m of a certain target
A function to match blank characters ([\t ])
Add a numbered capturing group around an expression
Makes all matches case insensitive, matching both upper and lowercase letters.
A function to match control characters ([\x00-\x1F\x7F])
A function for the digit character class (i.e., the digits 0 through 9)
A function to disable unicode support
A function that will allow . to match newlines (\n)
A function to match the end of text (or end-of-line with multi-line mode)
A function to match the end of text (even with multi-line mode enabled)
Escapes an entire list for use in something like an [or] or an [and] expression.
Match exactly n of a certain target
A function to match graphical characters ([!-~])
A function to match any digit that would appear in a hexadecimal number ([A-Fa-f0-9])
A function to match any lowercase character ([a-z])
Enables multiline mode, which will allow beginning() and end() to match the beginning and end of lines
Add a named capturing group around an expression
A function to match any non-alphabetic character ([^A-Za-z])
A function to match any non-alphanumeric character ([^0-9A-Za-z])
A function to match any non-ascii digit ([^\x00-\x7F])
A function to match non-blank characters ([^\t ])
A function to match non-control characters ([^\x00-\x1F\x7F])
A function for the non-digit character class (i.e., everything BUT the digits 0-9)
A function to match non-graphical characters ([^!-~])
A function to match any digit that wouldn’t appear in a hexadecimal number ([^A-Fa-f0-9])
A function to match any non-lowercase character ([^a-z])
A function to match unprintable characters ([^ -~])
A function to match non-punctuation ([^!-/:-@\[-{-~]`)
A function for not matching Unicode character categories. For matching script categories see non_unicode_script.
A function for matching Unicode characters not belonging to a certain script category. For matching other categories see non_unicode_category.
A function to match any non-uppercase character ([^A-Z])
A function for the whitespace character class (i.e., everything BUT space and tab)
A function for the non-word character class (i.e., everything BUT the alphanumeric characters plus underscore)
A function to match anything BUT a word boundary
This text is not escaped. You can use it, for instance, to add a regex string directly to the object.
Negated or relationship between two or more possible matches
Match one or more of a certain target
A function for establishing an OR relationship between two or more possible matches
A function to match printable characters ([ -~])
A function to match punctuation ([!-/:-@\[-{-~]`)
Subtracts the second argument from the first
Add matching text to the regex string. Text that is added through this function is automatically escaped.
A function for matching Unicode character categories. For matching script categories see unicode_script.
A function for matching Unicode characters belonging to a certain script category. For matching other categories see unicode_category.
A function to match any uppercase character ([A-Z])
A constant for the whitespace character class (i.e., space and tab)
Matches anything within a range of characters
Matches anything outside of a range of characters
A function for the word character class (i.e., all alphanumeric characters plus underscore)
A function to match a word boundary
Xor on two bracketed expressions, also known as symmetric difference.
Match zero or more of a certain target
Match zero or one of a certain target